package com.happok.onlineclass.usermanagement.user.service;

import cn.hutool.core.bean.BeanUtil;
import com.happok.onlineclass.currentuser.CurrentUser;
import com.happok.onlineclass.currentuser.CurrentUserResolver;
import com.happok.onlineclass.usermanagement.user.dataobject.UserPermissionDO;
import com.happok.onlineclass.usermanagement.user.dto.UpdateUserPermissionDTO;
import com.happok.onlineclass.usermanagement.user.dto.UpdateUsersPermissionDTO;
import com.happok.onlineclass.usermanagement.user.dto.UserAuthorityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/8/17 9:37
 */
@Service
public class UserPermissionServiceImpl implements UserPermissionService {

    @Autowired
    private UserPermissionDaoService userPermissionDaoService;

    @Autowired
    private UserDaoService userDaoService;

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void updateUserPermission(UpdateUsersPermissionDTO updateUsersPermissionDTO) {
        for (UpdateUserPermissionDTO updateUserPermissionDTO : updateUsersPermissionDTO.getUserPermissionList()) {
            if (updateUsersPermissionDTO.getUserPermissionList().size() == 1){
                userDaoService.isUserExist(updateUserPermissionDTO.getUserId());
            }
            UserPermissionDO userPermissionDO = new UserPermissionDO();
            BeanUtil.copyProperties(updateUserPermissionDTO, userPermissionDO);
            userPermissionDO.setOrgType(updateUserPermissionDTO.getOrgType().getOrgType());
            if (ObjectUtils.isEmpty(updateUserPermissionDTO.getPermissions())) {
                userPermissionDaoService.delete(userPermissionDO);
            } else {
                userPermissionDO.setPermission(String.join(",", updateUserPermissionDTO.getPermissions()));
                if (!userPermissionDaoService.isUserPermissionExist(updateUserPermissionDTO.getUserId(), updateUserPermissionDTO.getOrgId(), updateUserPermissionDTO.getOrgType().getOrgType())) {
                    userDaoService.isUserEnable(updateUserPermissionDTO.getUserId());
                    userPermissionDaoService.insert(userPermissionDO);
                } else {
                    userPermissionDaoService.update(userPermissionDO);
                }
            }
        }
    }

    @Override
    public UserAuthorityDTO getUserPermission(String user, HttpServletRequest req) {
        CurrentUser currentUser = CurrentUserResolver.getCurrentUser(user);
        UserDetails userDetails = userDetailsService.loadUserByUsername(currentUser.getId().toString());
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        context.setAuthentication(authentication);
        SecurityContextHolder.setContext(context);
        userDaoService.isUserExist(currentUser.getId());
        UserAuthorityDTO userAuthorityDTO = new UserAuthorityDTO();
        userAuthorityDTO.setEnterpriseAuthorityMap(userPermissionDaoService.enterpriseAuthorities(currentUser.getId()));
        userAuthorityDTO.setPlatformAuthoritys(userPermissionDaoService.platformAuthorities(currentUser.getId()));
        userAuthorityDTO.setTrainOrgAuthorityMap(userPermissionDaoService.trainOrgAuthorities(currentUser.getId()));
        return userAuthorityDTO;
    }
}